package Review.PriorityQueue;

import java.util.Arrays;

public class PriorityQueue {
    public int[] elem;
    public int usedSize;

    public PriorityQueue() {
        this.elem = new int[10];
    }

    /**
     * 建堆的时间复杂度：O(N)
     *
     * @param array
     */
    public void createHeap(int[] array) {
        for (int i = 0; i < array.length; i++) {
            this.elem[i] = array[i];
            usedSize++;
        }
    }

    /**
     *
     * @param root 是每棵子树的根节点的下标
     * @param len  是每棵子树调整结束的结束条件
     * 向下调整的时间复杂度：O(logn)
     */
    private void shiftDown(int root,int len) {
        int child = 2*root+1;
        while(child<len){
            if(child+1<len&&elem[child]<elem[child+1]){
                child++;
            }
            if (elem[child] > elem[root]) {
                swap(elem,child,root);
                child = root;
                child = 2*root+1;
            }
        }
    }
    private void swap(int[] elem,int i,int j){
        int tmp = elem[i];
        elem[i] = elem[j];
        elem[j] = tmp;
    }


    /**
     * 入队：仍然要保持是大根堆
     * @param val
     */
    public void push(int val) {
        if (isFull()){
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize] = val;
        usedSize++;

    }

    private void shiftUp(int child) {
        int parent = (child-1)/2;
        while (parent >= 0) {
            if (elem[child]>elem[parent]){
                swap(elem,child,parent);
                child =parent;
                parent = (child-1)/2;
            }
        }
    }

    public boolean isFull() {
        return usedSize==elem.length;
    }

    /**
     * 出队【删除】：每次删除的都是优先级高的元素
     * 仍然要保持是大根堆
     */
    public int pollHeap() {
        if (isEmpty()){
            return -1;
        }
        int val = elem[0];
        elem[0] = elem[--usedSize];
        shiftDown(0,usedSize-1);
        usedSize--;
        return  val;

    }

    public boolean isEmpty() {
        return usedSize==0;
    }

    /**
     * 获取堆顶元素
     * @return
     */
    public void peekHeap() {
        int end = usedSize-1;
        while (end>=0){
            swap(elem,0,end);
            shiftDown(0,end);
            end--;
        }
    }
    public static void insertSort(int[] array){
        // write code  here
        for (int i = 0; i < array.length; i++) {
            int tmp =array[i];
            int j =i-1;
            for (;j>0;j--){
                if (array[j]>tmp){
                    array[j+1] = array[j];
                }else{
                    array[j+1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }


        // 希尔排序
        public static void shellSort(int[] array){
        // write code  here
            int gap =array.length;
            while (gap>1){
                gap = gap/3+1;
                shell(array,gap);
            }
    }
    public static  void shell(int[] array,int gap){
        for (int i = gap; i <array.length ; i++) {
            int tmp =array[i];
            int j = i-gap;
            for(;j>0;j-=gap){
                if (array[j]>tmp){
                    array[j+gap] = array[j];
                }else{
                    array[j+gap] = tmp;
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }
}
